<?php
class Reg extends CI_Controller {
	public function __construct() {
		parent::__construct ();
		$this->load->helper ( array (
				'form',
				'url' 
		) );
	}
	
	/**
	 * 注册页面显示
	 */
	public function index($user_id = '') {

		$data = array();

		if(!empty($user_id))
		{
			$info = $this->user->get_user_info_id($user_id);

			if(empty($info))
			{
				exit ( $this->common->alert_msg_goback ( '参数错误' ) );
			}
			else
			{
				$data['up_account'] = $info['user_account'];
				$data['up_account_flag'] = '1';
			}
		}else
		{
			$data['up_account'] = 'dedaozhe';
			$data['up_account_flag'] = '0';
		}
		$this->load->helper ( 'form' );
		
		$this->load->view ( 'reg',$data );
	}
	
	/**
	 * 提交数据
	 */
	public function info_post() {
		if (empty ( $_POST ['mobile'] )) {
			
			exit ( $this->common->alert_msg_goback ( '参数错误' ) );
		}
		if (empty ( $_SESSION ['mobile'] )) {
			
			exit ( $this->common->alert_msg_goback ( '参数错误' ) );
		}
		if (empty ( $_SESSION ['code'] )) {
			
			exit ( $this->common->alert_msg_goback ( '参数错误' ) );
		}
		if($_POST ['mobile'] != $_SESSION['mobile'])
		{
			exit ( $this->common->alert_msg_goback ( '请输入正确的手机号' ) );
		}

		if($_POST ['check_code'] != $_SESSION['code'])
		{
			exit ( $this->common->alert_msg_goback ( '请输入正确的手机号' ) );
		}

		if($_POST['up_account'] != 'dedaozhe')
		{
			$up_user = $this->user->get_uesr_info($_POST['up_account']);

			if(empty($up_user) || $up_user['kick_status'] == 1)
			{
				exit ( $this->common->alert_msg_goback ( '推广账号不存在' ) );
			}

			$pid = $up_user['id'];

			$path = $up_user['path'];
		}
		else
		{
			$pid = 0;

			$path = '0/';
		}


		
		$mobile = $this->user->get_user_mobile($_POST['mobile']);

		if(!empty($mobile))
		{
			exit ( $this->common->alert_msg_goback ( '每个手机号只能注册一次' ) );
		}

		$info = $this->user->get_uesr_info ( $_POST ['user_account'] ); 
		
		if (! empty ( $info )) {
			
			exit ( $this->common->alert_msg_goback ( '公益账号已经存在' ) );
		} else {
			// 插入
			$result = $this->user->user_insert ($pid);
			
			if ($result) {

				$this->user->update_path($result,$path);

				$this->user->reg_digui_up_account($_POST['up_account']);
				
				exit ( $this->common->alert_msg_url ( '提交成功', site_url ( 'login' ) ) );
			} else {
				
				exit ( $this->common->alert_msg_goback ( '提交失败' ) );
			}
		}
	}

	// public function send_sms()
	// {
	// 	$mobile = '15942615329';

	// 	$r = rand(100000,999999);

	// 	$content = "您的验证码为：{$r}，请不要把验证码泄露给其他人。";

	// 	//$content = '您的验证码为：111111，请不要把验证码泄露给其他人。';

	// 	$target = "http://106.ihuyi.cn/webservice/sms.php?method=Submit";
		
	// 	$account = 'cf_liu123123';

	// 	$password = '6568892826a';

	// 	$apikey = '471f2d5e787125c3ca586b366928e64d';

	// 	$post_data = "account=$account&password=$password&mobile=".$mobile."&content=".rawurlencode($content);
	// 	//密码可以使用明文密码或使用32位MD5加密
	// 	$gets =  $this->common->xml_to_array($this->common->Post($post_data, $target));

	// 	if($gets['SubmitResult']['code']==2){

	// 	}
	// 	echo $gets['SubmitResult']['msg'];
	// }

	/**
	 * 发送校验码
	 */
	public function ajax_send_check_code()
	{

		

		if(empty($_POST['mobile']))
		{
			$res['err'] = -1;
			$res['msg'] = '手机号不能为空';
			$this->common->ajax_return($res);
		}

		$mobile = $_POST['mobile'];
		
		$r = rand(100000,999999);

		$content = "您的验证码为：{$r}，请不要把验证码泄露给其他人。";
		//echo $content;exit;
		$result = $this->common->send_sms($mobile,$content);

		if($result['SubmitResult']['code']==2)
		{
			$_SESSION['code'] = $r;

			$_SESSION['mobile'] = $mobile;

			$res['err'] = 1;

			$res['code'] = md5($r);
		}
		else
		{
			$res['err'] = -2;
			$res['result'] = $result;
			$res['msg'] = '提交失败';
		}
		//echo 1;exit;
		$this->common->ajax_return($res);
		//if($result[])
	}
}
